package com.yst.suivi.services.export.excel;

import java.io.IOException;
import java.io.OutputStream;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.read.biff.BiffException;
import jxl.write.WritableCellFormat;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import com.yst.suivi.services.pointage.PointageServiceBean;

public final class JxlUtil
{
    public static WritableWorkbook createWorkbook(OutputStream os, String template) throws IOException, BiffException
    {
        WorkbookSettings workbookSettings = new WorkbookSettings();
        workbookSettings.setSuppressWarnings(true);
        workbookSettings = new WorkbookSettings();
        workbookSettings.setFormulaAdjust(false);
        workbookSettings.setGCDisabled(true);
        workbookSettings.setIgnoreBlanks(false);
        workbookSettings.setMergedCellChecking(false);
        workbookSettings.setNamesDisabled(true);
        workbookSettings.setPropertySets(false);
        workbookSettings.setRationalization(false);
        Workbook workbookModel = Workbook.getWorkbook(PointageServiceBean.class.getResourceAsStream(template), workbookSettings);
        WritableWorkbook workbook = Workbook.createWorkbook(os, workbookModel, workbookSettings);
        return workbook;
    }

    /**
     * The method returns the format of a cell. The format is used for the headers cells.
     * 
     * @return
     * @throws WriteException
     */
    public static WritableCellFormat getHeadersFormat() throws WriteException
    {
        WritableCellFormat cellFormat = new WritableCellFormat();
        cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLUE_GREY);
        return cellFormat;
    }
}
